﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.MovieRecorder.CUiMRCRecorderWindow
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.MovieRecorder;

public class CUiMRCRecorderWindow : TxWindow, IComponentConnector
{
  private CApMRCRecorderViewModel _model;
  private CApMRCRibbonBarRecorderManager m_ribbonBarRecorderManager;
  private SettingsView m_settingsViewMode;
  private const string WINDOWS_EXPLORER = "explorer.exe";
  private const string WINDOWS_EXPLORER_ARGUMENTS_FORMAT = "/select, \"{0}\"";
  private const string SETTINGS_REGISTRY_PARAMETER = "Movie Recorder Settings";
  internal System.Windows.Controls.Button btnSettings;
  internal System.Windows.Controls.Button btnRecord;
  internal System.Windows.Controls.Button btnStop;
  internal System.Windows.Controls.Button btnDiscard;
  private bool _contentLoaded;

  public CUiMRCRecorderWindow()
  {
    this.InitializeComponent();
    this._model = new CApMRCRecorderViewModel();
    ((FrameworkElement) this).DataContext = (object) this._model;
    this.m_settingsViewMode = SettingsView.MovieRecorder;
    this.m_ribbonBarRecorderManager = new CApMRCRibbonBarRecorderManager();
    this.m_ribbonBarRecorderManager.ButtonStopClick += new EventHandler(this.m_ribbonBarRecorderManager_ButtonStopClick);
    this.m_ribbonBarRecorderManager.ButtonResetClick += new EventHandler(this.m_ribbonBarRecorderManager_ButtonResetClick);
    this.m_ribbonBarRecorderManager.ButtonPauseResumeClick += new EventHandler(this.m_ribbonBarRecorderManager_ButtonPauseResumeClick);
  }

  public CUiMRCRecorderWindow(CApMRCRecorderSettings settings)
    : this()
  {
    this._model = new CApMRCRecorderViewModel(settings);
    ((FrameworkElement) this).DataContext = (object) this._model;
  }

  public CUiMRCRecorderWindow(CApMRCRecorderSettings settings, SettingsView view)
    : this(settings)
  {
    this.m_settingsViewMode = view;
    this._model.HasPersistentSettings = false;
  }

  private void btnDiscard_Click(object sender, RoutedEventArgs e) => this.Discard();

  private void btnStop_Click(object sender, RoutedEventArgs e) => this.Stop();

  private void btnRecord_Click(object sender, RoutedEventArgs e)
  {
    bool flag = false;
    if (!this._model.IsRecording)
    {
      ((Window) this).Hide();
      flag = this.ShowSplashWindow();
      if (flag)
        this.MinimizeUIToRibbonBar();
      else
        ((Window) this).Show();
    }
    string errorMessage;
    if (this._model.DoAction(out errorMessage) || string.IsNullOrEmpty(errorMessage))
      return;
    int num = (int) TxMessageBox.ShowModal(errorMessage, MRCStringTable.MOVIE_RECORDER_COMMAND_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    if (!flag)
      return;
    this.RestoreUiFromRibbonBar();
  }

  private bool ShowSplashWindow()
  {
    CUiMRCRecorderSplashWindow recorderSplashWindow = new CUiMRCRecorderSplashWindow();
    new WindowInteropHelper((Window) recorderSplashWindow).Owner = TxApplicationEx.GetMainWindowHandle(IntPtr.Zero);
    recorderSplashWindow.ShowDialog();
    return recorderSplashWindow.MinimizeRecordingUIToRibbon;
  }

  private void btnSettings_Click(object sender, RoutedEventArgs e)
  {
    CApMRCRecorderSettings recorderSettings = new CApMRCRecorderSettings(this._model.Settings);
    bool? nullable = new CUiMRCRecorderSettingsWindow(recorderSettings, this.m_settingsViewMode).ShowDialog(true);
    if (nullable.HasValue && nullable.Value)
    {
      this._model.Settings = recorderSettings;
      this.SaveSettings();
      System.Windows.Forms.Cursor current = System.Windows.Forms.Cursor.Current;
      System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
      this._model.UpdateScene();
      System.Windows.Forms.Cursor.Current = current;
    }
    this._model.UpdateIndicator();
  }

  private void TxWindow_Loaded(object sender, RoutedEventArgs e)
  {
    this._model.StopRequested += new EventHandler(this._model_StopRequested);
    this._model.DurationChanged += new EventHandler<DurationChnagedEventArgs>(this._model_DurationChanged);
    this.LoadSettings();
    System.Windows.Forms.Cursor current = System.Windows.Forms.Cursor.Current;
    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
    this._model.UpdateScene();
    System.Windows.Forms.Cursor.Current = current;
    TxApplication.ApplicationEvents.ExitRequest += new TxApplication_ExitRequestEventHandler(this.ApplicationEvents_ExitRequest);
    this._model.UpdateIndicator();
  }

  private void _model_DurationChanged(object sender, DurationChnagedEventArgs e)
  {
    this.m_ribbonBarRecorderManager.UpdateDuration(e.CurrentDuration);
  }

  private void _model_StopRequested(object sender, EventArgs e)
  {
    this.Stop();
    this.RestoreUiFromRibbonBar();
  }

  private void LoadSettings()
  {
    if (!this._model.HasPersistentSettings)
      return;
    object xmlStringSettings;
    TxWindowSettings.RestoreWindowValue((TxWindow) this, "Movie Recorder Settings", ref xmlStringSettings);
    this._model.LoadSettingsFromXmlString(xmlStringSettings as string);
  }

  private void SaveSettings()
  {
    if (!this._model.HasPersistentSettings)
      return;
    TxWindowSettings.StoreWindowValue((TxWindow) this, "Movie Recorder Settings", (object) this._model.StoreSettingsToXmlString());
  }

  private void TxWindow_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  {
    if (e.Key != Key.Escape)
      return;
    ((Window) this).Close();
  }

  private void TxWindow_Closing(object sender, CancelEventArgs e)
  {
    if (this._model.IsRecording)
    {
      switch (TxMessageBox.ShowModal(MRCStringTable.MOVIE_RECORDER_MESSAGE_ONCLOSE, MRCStringTable.MOVIE_RECORDER_COMMAND_NAME, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk))
      {
        case DialogResult.Yes:
          this.Stop();
          break;
        case DialogResult.No:
          this.Discard();
          break;
        default:
          e.Cancel = true;
          break;
      }
    }
    if (e.Cancel)
      return;
    this._model.DurationChanged -= new EventHandler<DurationChnagedEventArgs>(this._model_DurationChanged);
    this._model.StopRequested -= new EventHandler(this._model_StopRequested);
    TxApplication.ApplicationEvents.ExitRequest -= new TxApplication_ExitRequestEventHandler(this.ApplicationEvents_ExitRequest);
    this._model.CloseIndicator();
    this.m_ribbonBarRecorderManager.RemoveFromRibbonBar();
    System.Windows.Forms.Cursor current = System.Windows.Forms.Cursor.Current;
    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
    this._model.RestoreOriginalScene();
    System.Windows.Forms.Cursor.Current = current;
  }

  private void TxWindow_Closed(object sender, EventArgs e)
  {
    this.SendNotification("Movie Recorder was closed");
  }

  private void Discard()
  {
    Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
    Mouse.UpdateCursor();
    this._model.Discard();
    Mouse.OverrideCursor = (System.Windows.Input.Cursor) null;
    Mouse.UpdateCursor();
  }

  private void Stop()
  {
    Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
    Mouse.UpdateCursor();
    this._model.Stop();
    Mouse.OverrideCursor = (System.Windows.Input.Cursor) null;
    Mouse.UpdateCursor();
    this.SendNotification("Movie was created | " + this._model.Settings.GetFileFullName(false));
    this.OpenMovieFileLocation();
  }

  private void OpenMovieFileLocation()
  {
    string str1 = string.Format(MRCStringTable.MOVIE_RECORDER_MESSAGE_SUCCESS, (object) this._model.Settings.GetFileFullName(false));
    if (TxDoNotShowAgainMessageBox.Show(str1, MRCStringTable.MOVIE_RECORDER_COMMAND_NAME, "Movie_Recorder_success_message", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, DialogResult.No) == DialogResult.Yes)
    {
      string str2 = $"/select, \"{this._model.Settings.GetFileFullName(false)}\"";
      ProcessStartInfo startInfo = new ProcessStartInfo();
      startInfo.FileName = "explorer.exe";
      startInfo.Arguments = str2;
      startInfo.WindowStyle = ProcessWindowStyle.Normal;
      try
      {
        Process.Start(startInfo);
      }
      catch (Win32Exception ex)
      {
      }
    }
    else
      TxApplication.StatusBarMessage = str1;
  }

  private void ApplicationEvents_ExitRequest(object sender, TxApplication_ExitRequestEventArgs args)
  {
    if (this._model.IsRecording)
    {
      int num = (int) TxMessageBox.Show(MRCStringTable.MOVIE_RECORDER_MESSAGE_ON_APP_CLOSE_REQUEST, MRCStringTable.MOVIE_RECORDER_COMMAND_NAME, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
      args.Approve = false;
    }
    else
      args.Approve = true;
  }

  private void m_ribbonBarRecorderManager_ButtonPauseResumeClick(object sender, EventArgs e)
  {
    string errorMessage;
    if (this._model.DoAction(out errorMessage) || string.IsNullOrEmpty(errorMessage))
      return;
    int num = (int) TxMessageBox.ShowModal(errorMessage, MRCStringTable.MOVIE_RECORDER_COMMAND_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
  }

  private void m_ribbonBarRecorderManager_ButtonResetClick(object sender, EventArgs e)
  {
    this.Discard();
    this.RestoreUiFromRibbonBar();
  }

  private void m_ribbonBarRecorderManager_ButtonStopClick(object sender, EventArgs e)
  {
    this.Stop();
    this.RestoreUiFromRibbonBar();
  }

  private void MinimizeUIToRibbonBar()
  {
    ((Window) this).Hide();
    this.m_ribbonBarRecorderManager.AddToRibbonBar();
  }

  private void RestoreUiFromRibbonBar()
  {
    ((Window) this).Show();
    this.m_ribbonBarRecorderManager.HideFromRibbonBar();
  }

  private void SendNotification(string notifyMessage)
  {
    // ISSUE: reference to a compiler-generated field
    // ISSUE: reference to a compiler-generated field
    // ISSUE: reference to a compiler-generated field
    // ISSUE: method pointer
    using (TxGlobalNotifier subscription = TxGlobalNotifier.CreateSubscription("Movie Recorder", "MovieRecording", CUiMRCRecorderWindow.\u003C\u003Ec.\u003C\u003E9__31_0 ?? (CUiMRCRecorderWindow.\u003C\u003Ec.\u003C\u003E9__31_0 = new TxGlobalNotifier.NotifierDelegate((object) CUiMRCRecorderWindow.\u003C\u003Ec.\u003C\u003E9, __methodptr(\u003CSendNotification\u003Eb__31_0)))))
      subscription.SendNotification(notifyMessage);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    System.Windows.Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/movierecorder/cuimrcrecorderwindow.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        ((FrameworkElement) target).Loaded += new RoutedEventHandler(this.TxWindow_Loaded);
        ((Window) target).Closing += new CancelEventHandler(this.TxWindow_Closing);
        ((UIElement) target).PreviewKeyDown += new System.Windows.Input.KeyEventHandler(this.TxWindow_PreviewKeyDown);
        ((Window) target).Closed += new EventHandler(this.TxWindow_Closed);
        break;
      case 2:
        this.btnSettings = (System.Windows.Controls.Button) target;
        this.btnSettings.Click += new RoutedEventHandler(this.btnSettings_Click);
        break;
      case 3:
        this.btnRecord = (System.Windows.Controls.Button) target;
        this.btnRecord.Click += new RoutedEventHandler(this.btnRecord_Click);
        break;
      case 4:
        this.btnStop = (System.Windows.Controls.Button) target;
        this.btnStop.Click += new RoutedEventHandler(this.btnStop_Click);
        break;
      case 5:
        this.btnDiscard = (System.Windows.Controls.Button) target;
        this.btnDiscard.Click += new RoutedEventHandler(this.btnDiscard_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
